package com.kylin.module.community.business;

/*
import java.text.SimpleDateFormat;
import java.text.ParseException;
*/
import java.util.Date;
import java.util.List;

import com.kylin.common.dict.orm.Dict;
import com.kylin.common.exception.BusinessException;

import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;

import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.TransactionDAO;
import com.kylin.module.community.forms.PostForm;
import com.kylin.module.community.orm.FriendslistDAO;
import com.kylin.module.community.orm.Member;
import com.kylin.module.community.orm.MemberDAO;
import com.kylin.module.community.orm.PostDAO;
import com.kylin.module.community.orm.Post;

import com.kylin.module.user.orm.User;

import org.apache.log4j.Logger;

import java.util.Locale;
import com.kylin.common.paginate.Paging;

public class PostBlogic {
	
	protected static Logger log = Logger.getLogger(PostBlogic.class);
	
	public Paging show(User user, Integer limitstart, Integer limit, PostForm postform, Locale loc){
		//log.info("user:" + user.getUsername());
		
		Paging page = new Paging();
		PostDAO fdao = new  PostDAO();
		
		page.setTotal(fdao.getTotal(user, postform, loc));

		page.setList(fdao.getList(user, limitstart, limit, postform, loc));
		page.setStart(limitstart);
		page.setLimit(limit);
		return page;		
	}		
	
	public Post save(User user, PostForm postform, Locale loc){
		
		log.info("in Post.save by "+user.getUsername());
		log.info("in Post.save, form.toString(): "+postform.toString());
		//try{
			log.info("get last 10 registers"+user.getUsername());
			
			Post model =  new Post();
			PostDAO dao = new PostDAO();

			model.setFkppostid(postform.getFkppostid());
			model.setTitle(postform.getTitle());
			model.setContent(postform.getContent());

			/* user in session */
			model.setUser(user);
			/*  set Catpost  */
			if (postform.getCatpostid() != null && postform.getCatpostid().intValue() != 0){
	 			//CatPostDAO  catpostdao =  new CatPostDAO();
				try{
					Dict catpost = (Dict) BaseHibernateDAO.findById(Dict.class, postform.getCatpostid());
					//CatPost catpost =  catpostdao.findById(postform.getCatpostid());
					model.setCatpost( catpost);
				}
				catch(Exception e){
					
				}
			}
			/*  set Tag  */
			if (postform.getTagid() != null && postform.getTagid().longValue() != 0){
	 			//TagDAO  tagdao =  new TagDAO();
				try{
					Dict tag = (Dict) BaseHibernateDAO.findById(Dict.class, postform.getTagid());
					model.setTag( tag);
				}catch(Exception e){
					
				}
			}

			model.setCreatedon(new Date());
			model.setActive(Post.WAIT);
			model.setHits(new Integer("0"));
			
			model.setLanguage(loc.toString());
			//model.setLanguage(user.getLanguage());
			model.setCreatedby(user.getUsername());
			
			dao.save(model);
			return model;
		//}
		//catch(BusinessException e){
		//}
	}
	
	public List getLastPost(Locale loc){
		log.info("get last 10 post");
		PostDAO pdao = new PostDAO();
		return pdao.findLast(loc);
	}	
	
	/*
	 * 
	 * 
	 */

	public List getPostByUserId(Long uid){
		try{
		PostDAO fdao = new PostDAO();
		User user = (User)BaseHibernateDAO.findById(User.class, uid);
		return fdao.list(user);
		}
		catch(Exception e){
			return null;
		}
	}	
	
	public List getPostByUser(User user){
		try{
		PostDAO fdao = new PostDAO();
		return fdao.list(user);
		}
		catch(Exception e){
			return null;
		}
	}		
	
	/*
	 * 
	 * 
	 */
	public void edit(Long id){
		try{
		User user = (User)BaseHibernateDAO.findById(User.class, id);
		log.info("get last 10 registers"+user.getUsername());
		PostDAO fdao = new PostDAO();
		//return fdao.findFriends(user);
		}
		catch(BusinessException e){
			//return null;
		}
	}	

	public Post findById(Long id){
		PostDAO fdao = new PostDAO();
		return fdao.findById(id);		
	}

	public void deleteById(Long id){
		PostDAO fdao = new PostDAO();
		Post post =  fdao.findById(id);
		fdao.delete(post);
	}	

	/*
	 * delete: ACTIVE_OFF
	 * 
	 */
	public void update(Long id){
		try{
			User user = (User)BaseHibernateDAO.findById(User.class, id);
			log.info("get last 10 registers"+user.getUsername());
			PostDAO fdao = new PostDAO();
			//return fdao.findFriends(user);
		}
		catch(BusinessException e){
			//return null;
		}
	}	
	
	/*
	 * 
	 * 
	 */
	public void add(User user, String content, Long pid){
		try{
		//User user = (User)BaseHibernateDAO.findById(User.class, userid);
		//User user2 = (User)BaseHibernateDAO.findById(User.class, user2id);
		
		log.info("get last 10 registers"+user.getUsername());
		PostDAO fdao = new PostDAO();
		fdao.add(user, content, pid);
		//return fdao.findFriends(user);
		}
		catch(Exception e){
			//return null;
		}
	}	 
	
}
